package com.javabatch.petstore.fixtures;

import static fitnesse.util.ListUtility.list;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.javabatch.fileio.FieldError;
import com.javabatch.fileio.FileBuffer;
import com.javabatch.fileio.FileStore;
import com.javabatch.fileio.RecordBasedFileStore;
import com.javabatch.petstore.records.CustomerRecord;

/**
 * Simple file generator for use with fitnesse.
 *
 * @author eco
 */
public class CustomerFileFixture {
    /** Customer file. */
    private static FileStore store;

    /** customer id. */
    private String id;

    /** Full name. */
    private String name;

    /** Post code. */
    private String postalCode;

    /** Customer address. */
    private String address;

    public void setId(String theId) {
        this.id = theId;
    }

    public void setName(String theName) {
        this.name = theName;
    }

    public void setPostalCode(String thePostalCode) {
        this.postalCode = thePostalCode;
    }

    public void setAddress(String theAddress) {
        this.address = theAddress;
    }

    /** Constructor to use from test when closing file. */
    public CustomerFileFixture() {
    }

    public CustomerFileFixture(final String file, final String readWrite) {

        if (readWrite.equalsIgnoreCase("R")) {
            store = new RecordBasedFileStore(file,
                    RecordBasedFileStore.READ_ACCESS,
                    CustomerRecord.getLengthAndValidateRecord());
        } else {
            store = new RecordBasedFileStore(file,
                    RecordBasedFileStore.WRITE_ACCESS,
                    CustomerRecord.getLengthAndValidateRecord());
        }
        store.open();
    }


    /**
     * Write text to file.
     *
     * @return true if ok
     */
    public String add() {
    

        FileBuffer outputRecord = new FileBuffer();

        CustomerRecord.CUSTOMER_ID.write(outputRecord, this.id);
        CustomerRecord.CUSTOMER_NAME.write(outputRecord, this.name);
        CustomerRecord.CUSTOMER_ADDRESS.write(outputRecord, this.address);
        CustomerRecord.CUSTOMER_POSTAL_CODE.write(outputRecord,
                this.postalCode);

        store.write(outputRecord);
        return "";
    }

    public List<Object> query() {
        List<FieldError> errors = new ArrayList<FieldError>();
        List<Object> rows = new LinkedList<Object>();
        FileBuffer inputRecord = store.readNext();

        while (inputRecord != null) {
            List<Object> aRow =
                    list(list("id", CustomerRecord.CUSTOMER_ID.read(inputRecord, errors).replaceAll("^0*", "")),
                         list("name", CustomerRecord.CUSTOMER_NAME.read(inputRecord, errors).trim()),
                         list("address", CustomerRecord.CUSTOMER_ADDRESS.read(inputRecord, errors).trim()),
                         list("postal code", CustomerRecord.CUSTOMER_POSTAL_CODE.read(inputRecord, errors).trim()));

            if (errors.size() == 0) {
                rows.add(aRow);
            } else {
                for (FieldError error : errors) {
                    rows.add(list(list("id", error.toString()),
                            list("name", ""), list("address", ""), list(
                                    "postal code", "")));
                }
            }

            inputRecord = store.readNext();
        }

        return rows;
    }

    public static boolean close() {
        store.close();
        return true;
    }
}
